// **********************************************//
// 系统公用的页面元素操作js
// @author 张卫东
// @date 2014-01-21
// **********************************************//
(function (window) {
    var window = window;
    var document = window.document;

    // 组织滚动条
    window.stopDefault = function (e) {
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
    }

    var uiUtils = {

    };
    window.uiUtils = uiUtils;

    /**
     * 获取id对应的元素
     */
    var $F = function (id) {
        if (!id) {
            return $("");
        }
        if (typeof id == 'string') {
            return $("#" + id);
        } else {
            return $(id);
        }
    }

    uiUtils.windowWidth = function () {
        return document.documentElement.clientWidth;
    }
    uiUtils.windowHeight = function () {
        return document.documentElement.clientHeight;
    }

    uiUtils.getWidth = function (id) {
        return $F(id).width();
    }

    uiUtils.getHeight = function (id) {
        return $F(id).height();
    }

    uiUtils.getX = function (id) {
        return $F(id).offset().left;
    }

    uiUtils.getY = function (id) {
        return $F(id).offset().top;
    }

    uiUtils.scrollTop = function () {
        var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        if (!scrollTop) {
            return 0;
        }
        return scrollTop;
    }

    uiUtils.scrollLeft = function () {
        var scrollLeft = document.documentElement.scrollLeft || window.pageXOffset || document.body.scrollLeft;
        if (!scrollLeft) {
            return 0;
        }
        return scrollLeft;
    }

    uiUtils.remove = function (id) {
        var goneElement = $F(id);
        goneElement.parentNode.removeChild(goneElement);
    }

    uiUtils.addMouseShow = function (mouseId, showId, left, top, panelId) {
        left = left ? left : 0;
        top = top ? top : 0;

        var mouse = $F(mouseId);
        var show = $F(showId);

        function sHide() {
            show.fadeOut(200);
        }

        function sShow() {
            show.fadeIn(200);
        }

        var id = Math.random();
        mouse.mouseleave(function () {
            window[id] = setTimeout(function () {
                sHide();
            }, 500);
        });
        mouse.mouseenter(function () {
            if (window[id]) {
                clearTimeout(window[id]);
                window[id] = null;
            }
            var x = uiUtils.getX(mouseId) + left + $("body").scrollLeft();
            var y = uiUtils.getY(mouseId) - uiUtils.getHeight(showId) + top - $("body").scrollTop();
            show.css("top", y).css("left", x);
            sShow();
        })
        show.mouseleave(function () {
            window[id] = setTimeout(function () {
                sHide();
            }, 500);
        });
        show.mouseenter(function () {
            if (window[id]) {
                clearTimeout(window[id]);
                window[id] = null;
            }
            sShow();
        })

    }
})(window);